<?php


namespace app\common\model;


use think\Db;
use think\Exception;
use think\Model;
use traits\model\SoftDelete;

class UserRollLog extends Model
{

    // 表名
    protected $name = 'user_roll_log';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'datetime';
    protected $dateFormat = 'Y-m-d H:i:s';

    // 定义时间戳字段名
    protected $createTime = 'create_dt';
    protected $updateTime = 'update_dt';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
    ];


    public static function useRoll($roll,$play_no)
    {
        Db::startTrans();
        try {
            $roll->status = 2;
            $roll->save();
            self::create([
                'user_id' => $roll['user_id'],
                'roll_type' => $roll['roll_type'],
                'roll_id' => $roll['id'],
                'use_num' => 1,
                'play_no' => $play_no,
            ]);
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            return false;
        }
    }


    public static function useRolls($rolls,$play_no)
    {
        Db::startTrans();
        try {
            foreach ($rolls as $key => $value) {
                $value->status = 2;
                $value->save();
                self::create([
                    'user_id' => $value['user_id'],
                    'roll_type' => $value['roll_type'],
                    'roll_id' => $value['id'],
                    'use_num' => 1,
                    'play_no' => $play_no,
                ]);
            }
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            return false;
        }
    }
}